{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install tensorboard tensorflow pandas\n",
    "\n",
    "\"\"\"\n",
    "If tensorboard is not installed (or other dependencies, such as tensorflow and pandas),\n",
    "uncomment the command in top and re-run. This needs only to be run once in a Jupyter kernel.\n",
    "\"\"\"\n",
    "\n",
    "%load_ext tensorboard\n",
    "\n",
    "from tensorflow.python.summary.summary_iterator import summary_iterator\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "      <iframe id=\"tensorboard-frame-2e852c5a7b4534aa\" width=\"100%\" height=\"800\" frameborder=\"0\">\n",
       "      </iframe>\n",
       "      <script>\n",
       "        (function() {\n",
       "          const frame = document.getElementById(\"tensorboard-frame-2e852c5a7b4534aa\");\n",
       "          const url = new URL(\"/\", window.location);\n",
       "          const port = 6006;\n",
       "          if (port) {\n",
       "            url.port = port;\n",
       "          }\n",
       "          frame.src = url;\n",
       "        })();\n",
       "      </script>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Change the LOG_DIR argument to point to the correct directory, you may want to use an\n",
    "absolute path if you run into issues.\n",
    "\"\"\"\n",
    "%tensorboard --logdir ../logging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def logs_to_pandas(path: str) -> pd.DataFrame:\n",
    "    \"\"\"convert single tensorflow log file to pandas DataFrame\n",
    "    Parameters\n",
    "    ----------\n",
    "    path : str\n",
    "        path to tensorflow log file\n",
    "    Returns\n",
    "    -------\n",
    "    pd.DataFrame\n",
    "        converted dataframe\n",
    "    \"\"\"\n",
    "\n",
    "    runlog_data = pd.DataFrame({\"metric\": [], \"value\": [], \"step\": [], \"wall_time\": []})\n",
    "    try:\n",
    "        event_acc = summary_iterator(path)\n",
    "        for event in list(event_acc)[1:]:\n",
    "            step, wall_time = event.step, pd.to_datetime(event.wall_time, unit='s')\n",
    "            simple_extractor = [{\"metric\": v.tag, \"value\": v.simple_value, \"step\": step, 'wall_time': wall_time} for v in event.summary.value]\n",
    "            event_r = pd.DataFrame(simple_extractor)\n",
    "            runlog_data = pd.concat([runlog_data, event_r])\n",
    "    # Dirty catch of DataLossError\n",
    "    except Exception as e:\n",
    "        print(\"Event file possibly corrupt: {}\".format(path))\n",
    "        print(e)\n",
    "    return runlog_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/jeroen/fltk-testbed/fltk-testbed/venv/lib/python3.9/site-packages/tensorflow/python/summary/summary_iterator.py:31: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use eager execution and: \n",
      "`tf.data.TFRecordDataset(path)`\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>metric</th>\n",
       "      <th>value</th>\n",
       "      <th>step</th>\n",
       "      <th>wall_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>training loss per epoch</td>\n",
       "      <td>0.358933</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2021-09-28 07:41:37.473129472</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>accuracy per epoch</td>\n",
       "      <td>87.699997</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2021-09-28 07:41:37.473197568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>training loss per epoch</td>\n",
       "      <td>0.265972</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2021-09-28 07:42:04.857327872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>accuracy per epoch</td>\n",
       "      <td>88.370003</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2021-09-28 07:42:04.857392384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>training loss per epoch</td>\n",
       "      <td>0.266854</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2021-09-28 07:42:32.819394048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>accuracy per epoch</td>\n",
       "      <td>89.779999</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2021-09-28 07:42:32.819455744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>training loss per epoch</td>\n",
       "      <td>0.250556</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2021-09-28 07:43:19.743513088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>accuracy per epoch</td>\n",
       "      <td>90.129997</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2021-09-28 07:43:19.743613696</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    metric      value  step                     wall_time\n",
       "0  training loss per epoch   0.358933   1.0 2021-09-28 07:41:37.473129472\n",
       "0       accuracy per epoch  87.699997   1.0 2021-09-28 07:41:37.473197568\n",
       "0  training loss per epoch   0.265972   2.0 2021-09-28 07:42:04.857327872\n",
       "0       accuracy per epoch  88.370003   2.0 2021-09-28 07:42:04.857392384\n",
       "0  training loss per epoch   0.266854   3.0 2021-09-28 07:42:32.819394048\n",
       "0       accuracy per epoch  89.779999   3.0 2021-09-28 07:42:32.819455744\n",
       "0  training loss per epoch   0.250556   4.0 2021-09-28 07:43:19.743513088\n",
       "0       accuracy per epoch  90.129997   4.0 2021-09-28 07:43:19.743613696"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = \"../logging/cloud_experiment_0_FashionMNISTCNN_ee232974-dcde-4977-8f3d-40bf1accabb2/events.out.tfevents.1632814874.not-ubuntu.74346.0\"\n",
    "\n",
    "logs_to_pandas(path)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PyCharm (fltk-testbed)",
   "language": "python",
   "name": "pycharm-d7d3f210"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
